#ifndef FORMSLAVEPARAM_H
#define FORMSLAVEPARAM_H

#include <QWidget>
#include <dialogslaveparamread.h>
#include <dialogslaveparamwrite.h>
#include <dialogvkb.h>
#include <dcsdboperator.h>
#include "dcsproto/proto.h"
#include "udpworker.h"

namespace Ui
{
    class FormSlaveParam;

    //时钟频率转换表，（值对应的索引+1）* 10 = 百分比
    constexpr const static quint8 mClkFreqConvertTable[9] = {0x19, 0x33, 0x4d, 0x66, 0x80, 0x9a, 0xb3, 0xcd, 0xe6};
    constexpr static int TlbSize = 16;
    constexpr static unsigned short ChipIDTable[] = {
            EN_LED_CHIP_TYPE_SM16511,
            EN_LED_CHIP_TYPE_SM16512,
            EN_LED_CHIP_TYPE_UCS512C,
            EN_LED_CHIP_TYPE_UCS512C4,
            EN_LED_CHIP_TYPE_UCS512D,
            EN_LED_CHIP_TYPE_UCS512E,
            EN_LED_CHIP_TYPE_UCS512F,
            EN_LED_CHIP_TYPE_DMX512,
            EN_LED_CHIP_TYPE_DMX05,
            EN_LED_CHIP_TYPE_SM18522,
            EN_LED_CHIP_TYPE_UCS5603,
            EN_LED_CHIP_TYPE_UCS2903,
            EN_LED_CHIP_TYPE_UCS8904,
            EN_LED_CHIP_TYPE_UCS8903,
            EN_LED_CHIP_TYPE_TM1804,
            EN_LED_CHIP_TYPE_TM1803
    };
}

#define MAX_CHANNEL_NUM 3072        //单端口最大通道数
#define DEFAULT_CHANNEL_NUM 512     //端口默认通道数

#define MAX_GAMMA_VALUE 8.8f        //最大gamma值
#define DEFAULT_GAMMA_VALUE 2.2f    //默认gamma值

#define MAX_CLKFREQ_VALUE   8.0f    //最大
#define DEFAULT_CLKFREQ_VALUE 0.25f

class FormSlaveParam : public QWidget
{
Q_OBJECT

public:
    explicit FormSlaveParam(QWidget *parent = nullptr);

    ~FormSlaveParam() override;

    static int getChipIndexByChipType(unsigned short chiptype);

    static quint16 getChipTypeByChipIndex(int chipid);

    void fillGamaData(unsigned short *pAddr, int gamaVal) const;

    static quint8 dutyrateConvert2Percent(quint8 value);

    static quint8 percentConvert2Dutyrate(quint8 percent);

private slots:

    void show_numberkeyboard_ui(const QString &data);     //显示键盘

    void confirmString(const QString &gemfield);   //接收键盘发过来的数据

    void on_pushButtonReturn_clicked();

    void on_pushButtonRead_clicked();

    void on_readDialogCommit();

    void on_writeDialogCommit();

    void on_pushButtonWrite_clicked();

    void on_horizontalSliderBrightness_valueChanged(int value);

    void on_horizontalSliderBrightness1_valueChanged(int value);

    void on_horizontalSliderBrightness2_valueChanged(int value);

    void on_horizontalSliderBrightness3_valueChanged(int value);

    void on_horizontalSliderBrightness4_valueChanged(int value);

    void on_dataRecved(QByteArray &data);

    void onChipTypeIndexChanged(int index);

    void onChannelChanged(const QString &text);

    void onClockFreqChanged(const QString &text);

    void onDutyRateChanged(int index);

    void onColorChanged(int index);

    void onGammaChanged(const QString &text);

private:
    Ui::FormSlaveParam *ui;
    DialogSlaveParamRead *slaveParamReadWin{};
    DialogSlaveParamWrite *slaveParamWriteWin{};
    DCSDbOperator *mDbopt{};
    DialogVKB *numberkeyboard;
    QString lineEdit_object;//判断哪个lineedit调用了软件盘
    UdpWorker *mUdp;

    struct CurIC
    {
        int chipTypeId;
        int chipIndex;
    } mCurIC{2, EN_LED_CHIP_TYPE_UCS512C};
    unsigned short mChannelNum{DEFAULT_CHANNEL_NUM};         /*端口通道数*/
    unsigned char mGrayLvl{0};             /*灰度等级*/
    unsigned short mClockFreq{320};          /*时钟频率*/
    unsigned short mClockFreqRead{320};
    unsigned char mCh0FineTune{0x0A};
    unsigned char mDutyRate{0x80};            /*占空比*/
    unsigned char mGrayFilter{0};           /*低灰过滤*/
    unsigned char mColorNum{4};            /*颜色数*/
    unsigned char mOe{0};
    unsigned char mGammaTuneType{0};      /*伽玛值设置方式，0表示通道整体设置，1表示通道分别设置，其他值无效，默认为0*/
    unsigned short mGamma[4]{2200, 2200, 2200, 2200};
    unsigned char mBrightnessTuneType{0}; /*亮度调节设置方式，0表示通道亮度整体设置，1表示通道亮度分别设置，其他值无效，默认为0*/
    unsigned short mBrightness[4]{0x0100, 0x0100, 0x0100, 0x0100};
    unsigned char mRefleshType{1};         /*刷新频率方式选择，0表示手动设置刷新频率，1表示自动设置刷新频率，其他值无效，默认为1*/
    unsigned char mRefleshValue{0};        /*刷新频率数值，刷新频率范围会根据芯片类型、带载、频率、灰度等级等因素自动调整，默认值根据芯片而定*/
    unsigned char mCurrentGain[4]{0, 0, 0, 0};      /*电流增益值，默认值待定，分别代表A、B、C、D四通道的电流数值；*/
    unsigned char mOutputInverse{0};       /*输出反向，0表示不反向，1表示反向，默认为0不反向；*/
    unsigned char mAddrPinLvl{1};         /*地址线默认电平为高，0表示为低，1表示为高，默认为1*/
    unsigned char mLedHold{0};             /*信号断开显示状态，0表示保留最后一帧，1表示黑屏，默认为为0*/
};

#endif // FORMSLAVEPARAM_H
